package com.cherry.controller;

import com.cherry.entity.Goods;
import com.cherry.entity.ResultData;
import com.cherry.service.IGoodsService;
import com.cherry.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @create 2020-09-02 10:05 下午
 * @function:
 **/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    /**
     * 返回当前展示的秒杀场次
     *
     * @return
     */
    @GetMapping("/times")
    public ResultData<List<String>> getSecKillTime() {
        List<String> times = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            String time = DateUtil.getTime(i);
            log.info("场次计算成功：" + time);
            times.add(time);
        }
        return new ResultData<List<String>>().setData(times);
    }

    /**
     * 根据场次获取秒杀商品列表
     *
     * @param time
     * @return
     */
    @RequestMapping("/listByTime")
    public ResultData<List<Goods>> getGoodsByTime(String time) {
        System.out.println("获得秒杀场次：" + time);
        List<Goods> data = goodsService.getSecGoodsListByTime(time);
        return new ResultData<List<Goods>>().setData(data);
    }

    /**
     * 根据id查询商品信息
     *
     * @return
     */
    @RequestMapping("/getGoodsById")
    public ResultData<Goods> getGoodsById(Integer gid) {
        Goods goods = goodsService.getById(gid);
        return new ResultData<Goods>().setData(goods);
    }

    /**
     * 获得服务器当前时间
     *
     * @return
     */
    @RequestMapping("/now")
    public ResultData<Date> now() {
        return new ResultData<Date>().setData(new Date());
    }
}
